﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Deferred.Editor.Gui
{
    public static class WindowFlasher
    {
        // flash the taskbar button
        private static Int32 FLASHW_TIMER = 4;

        // flash until the window comes to the foreground 
        private static Int32 FLASHW_TIMERNOFG = 12;

        [DllImport("user32.dll")]
        private static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);

        [StructLayout(LayoutKind.Sequential)]
        private struct FLASHWINFO
        {
            public UInt32 cbSize;
            public IntPtr hwnd;
            public Int32 dwFlags;
            public UInt32 uCount;
            public Int32 dwTimeout;
        }

        public static void Flash(Form form)
        {
            FLASHWINFO fw = new FLASHWINFO();

            fw.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(FLASHWINFO)));
            fw.hwnd = form.Handle;
            fw.dwFlags = FLASHW_TIMER | FLASHW_TIMERNOFG;
            fw.uCount = UInt32.MaxValue;

            FlashWindowEx(ref fw);
        }
    }
}
